using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;
using EntGuayasSLA.BL.Listas.Criterias;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class ValidacionFicha : BusinessBase<ValidacionFicha>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(p => p.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        public static readonly PropertyInfo<string> ExpedienteProperty = RegisterProperty<string>(c => c.Expediente);
        public string Expediente
        {
            get { return GetProperty(ExpedienteProperty); }
            set { SetProperty(ExpedienteProperty, value); }
        }
        public static readonly PropertyInfo<string> CategoriaIdProperty = RegisterProperty<string>(c => c.CategoriaId);
        public string CategoriaId
        {
            get { return GetProperty(CategoriaIdProperty); }
            set { SetProperty(CategoriaIdProperty, value); }
        }
     
        public static readonly PropertyInfo<string> ValidoProperty = RegisterProperty<string>(c => c.Valido);
        public string Valido
        {
            get { return GetProperty(ValidoProperty); }
            set { SetProperty(ValidoProperty, value); }
        }
   
        public static readonly PropertyInfo<string> ComentarioProperty = RegisterProperty<string>(c => c.Comentario);
        public string Comentario
        {
            get { return GetProperty(ComentarioProperty); }
            set { SetProperty(ComentarioProperty, value); }
        }
        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion
     
        #region Factory Methods

        public static ValidacionFicha NewEditableRoot()
        {
            return DataPortal.Create<ValidacionFicha>();
        }
        public static ValidacionFicha GetEditableRoot(string ExpedienteIdTmp, string IdPregunta)
        {
            ValidaFichaCriteria criteria = new ValidaFichaCriteria(ExpedienteIdTmp, IdPregunta);
            return DataPortal.Fetch<ValidacionFicha>(criteria);
        }
        public static ValidacionFicha GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<ValidacionFicha>(id);
        }

        public static void DeleteEditableRoot(Guid id)
        {
            DataPortal.Delete<ValidacionFicha>(id);
        }

        private ValidacionFicha()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }
        private void DataPortal_Fetch(ValidaFichaCriteria criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_exp_id", criteria.Expediente),
                new Parametro("ec_cte_id", criteria.PreguntaId)
                 };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_VALFICHA_EXP\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {

                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["vaf_id"].ToString()));
                LoadProperty(CategoriaIdProperty, datos.Tables[0].Rows[0]["cae_id"].ToString());
                LoadProperty(ExpedienteProperty, datos.Tables[0].Rows[0]["exp_id"].ToString());
                LoadProperty(ValidoProperty, datos.Tables[0].Rows[0]["vaf_valido"].ToString());
                LoadProperty(ComentarioProperty, datos.Tables[0].Rows[0]["vaf_comentario"].ToString());

            }
        }
        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_vaf_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_VALFICHA\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {             

                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["vaf_id"].ToString()));
                LoadProperty(CategoriaIdProperty, datos.Tables[0].Rows[0]["cae_id"].ToString());
                LoadProperty(ExpedienteProperty, datos.Tables[0].Rows[0]["exp_id"].ToString());                
                LoadProperty(ValidoProperty, datos.Tables[0].Rows[0]["vaf_valido"].ToString());      
                LoadProperty(ComentarioProperty, datos.Tables[0].Rows[0]["vaf_comentario"].ToString());
             
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_vaf_id", ReadProperty(IdProperty)),
                new Parametro("ec_exp_id", ReadProperty(ExpedienteProperty)),
                new Parametro("ec_cte_id", ReadProperty(CategoriaIdProperty)),             
                new Parametro("ec_vaf_valido", ReadProperty(ValidoProperty)),              
                new Parametro("ec_vaf_comentario", ReadProperty(ComentarioProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_VALFICHA\"", parametros);

        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
        
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
               new Parametro("ec_vaf_id", ReadProperty(IdProperty)),
               new Parametro("ec_cte_id", ReadProperty(CategoriaIdProperty)),             
               new Parametro("ec_vaf_valido", ReadProperty(ValidoProperty)),              
               new Parametro("ec_vaf_comentario", ReadProperty(ComentarioProperty))
             
            };
            accesoDatos.EjecutarQuery("\"ACT_VALFICHA\"", parametros);
     
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.Id);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_vaf_id", ReadProperty(IdProperty))
            };
            accesoDatos.EjecutarQuery("\"ELI_VALFICHA\"", parametros);
      
        }

        #endregion

       
    }
}
